import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    host: '0.0.0.0',
    port: 8090,
    strictPort: false,
    https: {
      // 使用您的证书文件
      key: './ssl/lzetyy.com.key',
      cert: './ssl/lzetyy.com.pem'
      // 如果证书文件不存在，Vite会自动使用自签名证书
    },
    // API代理配置 - 保持开发环境与部署环境一致
    proxy: {
      '/refundtask/api': {
        target: 'https://www.lzetyy.com:6724',
        changeOrigin: true,
        secure: true,
        rewrite: (path) => path.replace(/^\/refundtask\/api/, ''),
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('proxy error', err);
          });
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('Sending Request to the Target:', req.method, req.url);
          });
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('Received Response from the Target:', proxyRes.statusCode, req.url);
          });
        }
      }
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    sourcemap: false,
    rollupOptions: {
      output: {
        manualChunks: {
          vendor: ['vue', 'vue-router', 'pinia'],
          bootstrap: ['bootstrap'],
          utils: ['axios', 'crypto-js', 'xlsx']
        }
      }
    },
    // 企业微信H5应用优化
    target: 'es2015',
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  },
  base: '/refundtask/',
  // 企业微信环境配置
  define: {
    __WECHAT_WORK_ENV__: JSON.stringify(process.env.NODE_ENV === 'production')
  }
})